﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace HomeWork7
{
    public partial class Form1 : Form
    {
        private Panel panel = new Panel();
        private Graphics graphics=null;
        int n = 10;
        double leng = 100;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        Color penColor = Color.Blue;
        
        void DrawCayleyTree(int n,double x0,double y0,double leng, double th)
        {
            if (n == 0) return;
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);
            DrawLine(x0, y0, x1, y1);
            DrawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            DrawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void DrawLine(double x0,double y0,double x1,double y1)
        {
            Pen pen = new Pen(penColor);
            graphics.DrawLine(pen,(int)x0, (int)y0, (int)x1, (int)y1);
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {          
            if (graphics == null) graphics = this.pnlDrawPanel.CreateGraphics();
            graphics.Clear(Color.White);
            DrawCayleyTree(n, 200, 310, leng, -Math.PI / 2);
            
        }

        private void pnlDrawPanel_Paint(object sender, PaintEventArgs e)
        {
            
        }

        private void txtN_TextChanged(object sender, EventArgs e)
        {
            
            try
            {
                int N = 0;
                if (txtN.Text.Length > 0)
                {
                    N = Convert.ToInt32(txtN.Text);
                    if (N > 0) { n = N; lblError.Text = ""; }
                    else lblError.Text = "递归长度是正整数";
                }
                else lblError.Text = "";
            }
            catch
            {
                lblError.Text = "递归长度是正整数";
            }
        }

        private void txtLeng_TextChanged(object sender, EventArgs e)
        {
            try
            {
                double Leng = 0;
                if (txtLeng.Text.Length > 0)
                {
                    Leng = Convert.ToDouble(txtLeng.Text);
                    if (Leng > 0) { leng = Leng; lblError.Text = ""; }
                    else lblError.Text = "主干长度是正数";
                }
                else lblError.Text = "";
            }
            catch
            {
                lblError.Text = "主干长度是正数";
            }
        }

        private void tkbPer1_Scroll(object sender, EventArgs e)
        {
            per1 = tkbPer1.Value/10.0;
           
        }

        private void tkbPer2_Scroll(object sender, EventArgs e)
        {
            per2 = tkbPer2.Value/10.0;
        }

        private void tkbTh1_Scroll(object sender, EventArgs e)
        {
            th1 = tkbTh1.Value*Math.PI/180;
        }

        private void tkbTh2_Scroll(object sender, EventArgs e)
        {
            th2 = tkbTh2.Value * Math.PI / 180;
        }

        private void cbxPenColor_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cbxPenColor.Text)
            {
             case "Black":penColor = Color.Black;break;
             case "Blue":penColor = Color.Blue; break;
             case "Red":penColor = Color.Red; break;
             case "Green":penColor = Color.Green; break;
             case "Orange":penColor = Color.Orange; break;
        
        }
        }
    }
}
